package com.sjtu.rest;

import com.sjtu.config.Admin;
import com.sjtu.config.AdminHandlerFilter;
import com.sjtu.feign.UserAccountFeign;
import com.sjtu.web.BaseRest;
import com.sjtu.web.Result;
import com.sjtu.web.form.UserAccountForm;
import com.sjtu.web.form.UserAccountResetPwdForm;
import com.sjtu.web.search.UserAccountSearch;
import com.sjtu.web.vo.UserAccountVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户账户
 * nerine
 * 1/29/2020
 */
@RequestMapping("/admin/user-account")
@RestController
public class UserAccountRest extends BaseRest {
    @Autowired
    private UserAccountFeign userAccountFeign;

    @Admin
    @PostMapping("/list")
    public Result<?> list(@Valid @RequestBody UserAccountSearch search) {
        return userAccountFeign.list(search);
    }

    @Admin
    @PostMapping("/save")
    public Result<?> save(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId,
                          @RequestBody @Valid UserAccountForm form) {
        form.setOperatorId(operatorId);
        if(StringUtils.isBlank(form.getPassword())) {
            form.setPassword(null);
        }
        return userAccountFeign.save(form);
    }
    @PostMapping("/get-role")
    public Result<?> getRole(@RequestHeader(AdminHandlerFilter.OPERATOR_ID) String operatorId) {
        UserAccountVo vo = userAccountFeign.findByUserId(operatorId).getData();
        return success(vo.getRole());
    }
    @PostMapping("/reset-pwd")
    public Result<?> resetPwd(@RequestBody @Valid UserAccountResetPwdForm form) {
        return userAccountFeign.resetPwd(form);
    }
    @Admin
    @GetMapping("/detail/{id}")
    public Result<?> detail(@PathVariable String id) {
        return userAccountFeign.detail(id);
    }
}
